package com.shw.system.component;

/**
 * <事务管理器配置>
 * 无需加@EnableTransactionManagement注解，SpringBoot自动配置
 * 
 * @author wangxu
 * @version 1.0
 * @date 2020/04/19 22:53
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.sql.DataSource;

/**
 * 事务配置
 */
@Configuration
public class SystemConfiguration
{
    @Autowired
    DataSource dataSource;
    
    @Bean("transactionManager")
    public DataSourceTransactionManager mybatisTransactionManager()
    {
        return new DataSourceTransactionManager(dataSource);
    }
    
    @Bean
    @Order(0)
    public CorsFilter corsFilter()
    {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(Boolean.valueOf(true));
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.setMaxAge(Long.valueOf(18000L));
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
